$(document).ready(function() {
    Tipped.create('#comment-iconof-password', "为账号设置好密码，以后可以通过密码直接登录。注意：请记住密码！", {skin:'red'});
});

function firsttimepwdShowErrorInfo(info) {
    var informationHandle = $('#check-error-result');
    var informationContainerHandle = $('#error-info-container');

    informationContainerHandle.text(info);
    informationHandle.css('display', 'block');
}

function firsttimeToggleInputAndButton(open) {
    $('#password-input').prop('disabled', open);
    $('#password-retype-input').prop('disabled', open);
    $('#finish-button').prop('disabled', open);
}

function firsttimepwdOnFinishClick(target) {
    var account = $('#readonly-account').val();
    var password = $('#password-input').val();
    var password2 = $('#password-retype-input').val();

    if (password == undefined || password == '') {
        firsttimepwdShowErrorInfo("密码输入不能是空");
        return;
    }

    if (password != password2) {
        firsttimepwdShowErrorInfo("输入的密码不一致");
        return;
    }

    firsttimeToggleInputAndButton(true);

    var str = JSON.stringify({"account": account, "password": password});
    var key = makeKey(16);
    var encrypted = CryptoJS.AES.encrypt(str, key).toString();

    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(publicKey);
    key = encrypt.encrypt(key);

    var count = 0;
    var tips = '正在为您跳转';

    $.ajax(
        {
            type: 'post',
            async: true,
            data: {'account': encrypted, 'key': key},
            url: document.location.origin + '/api/registerbyemail',
            dataType:'text',
            success: function(data) {
                var json = JSON.parse(data);
                if (json.result == 200) {
                    setCookie("token", json.token, 30);

                    setInterval(function() {
                        count++;
                        tips = tips + '.';
                        $('#label-jumping-tip').text(tips);
                        $('#jumping-label-container').css('display', 'block');
                        if (count == 5) {
                            window.location.href = document.location.origin + "/usercenteraccount";
                        }
                    }, 1000);
                }
                else {
                    firsttimepwdShowErrorInfo(json.description);
                    firsttimeToggleInputAndButton(false);
                }
            },
            error: function () {
                console.log("Ajax 发生错误！");
                firsttimeToggleInputAndButton(false);
            }
        }
    );
}

